<template>
  <el-dialog
     v-drag
    :title="!dataForm.itemId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单id" prop="orderId">
      <el-input size="mini"  v-model="dataForm.orderId" placeholder="订单id" clearable></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="orderNo">
      <el-input size="mini"  v-model="dataForm.orderNo" placeholder="订单编号" clearable></el-input>
    </el-form-item>
    <el-form-item label="药品Id" prop="goodsId">
      <el-input size="mini"  v-model="dataForm.goodsId" placeholder="药品Id" clearable></el-input>
    </el-form-item>
    <el-form-item label="药品编号" prop="goodsCode">
      <el-input size="mini"  v-model="dataForm.goodsCode" placeholder="药品编号" clearable></el-input>
    </el-form-item>
    <el-form-item label="药品名称" prop="goodsName">
      <el-input size="mini"  v-model="dataForm.goodsName" placeholder="药品名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="通用名" prop="genericName">
      <el-input size="mini"  v-model="dataForm.genericName" placeholder="通用名" clearable></el-input>
    </el-form-item>
    <el-form-item label="规格" prop="standard">
      <el-input size="mini"  v-model="dataForm.standard" placeholder="规格" clearable></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input size="mini"  v-model="dataForm.unit" placeholder="单位" clearable></el-input>
    </el-form-item>
    <el-form-item label="产地" prop="producingArea">
      <el-input size="mini"  v-model="dataForm.producingArea" placeholder="产地" clearable></el-input>
    </el-form-item>
    <el-form-item label="生产单位" prop="produceFactory">
      <el-input size="mini"  v-model="dataForm.produceFactory" placeholder="生产单位" clearable></el-input>
    </el-form-item>
    <el-form-item label="批准文号" prop="approvalNo">
      <el-input size="mini"  v-model="dataForm.approvalNo" placeholder="批准文号" clearable></el-input>
    </el-form-item>
    <el-form-item label="剂型" prop="dosageForm">
      <el-input size="mini"  v-model="dataForm.dosageForm" placeholder="剂型" clearable></el-input>
    </el-form-item>
    <el-form-item label="批号" prop="batchNo">
      <el-input size="mini"  v-model="dataForm.batchNo" placeholder="批号" clearable></el-input>
    </el-form-item>
    <el-form-item label="生产日期" prop="produceDate">
      <el-input size="mini"  v-model="dataForm.produceDate" placeholder="生产日期" clearable></el-input>
    </el-form-item>
    <el-form-item label="有效期" prop="expiredDate">
      <el-input size="mini"  v-model="dataForm.expiredDate" placeholder="有效期" clearable></el-input>
    </el-form-item>
    <el-form-item label="采购数量" prop="purchaseNum">
      <el-input size="mini"  v-model="dataForm.purchaseNum" placeholder="采购数量" clearable></el-input>
    </el-form-item>
    <el-form-item label="采购价" prop="purchasePrice">
      <el-input size="mini"  v-model="dataForm.purchasePrice" placeholder="采购价" clearable></el-input>
    </el-form-item>
    <el-form-item label="采购金额" prop="purchaseAmount">
      <el-input size="mini"  v-model="dataForm.purchaseAmount" placeholder="采购金额" clearable></el-input>
    </el-form-item>
    <el-form-item label="上一次进价" prop="lastInPrice">
      <el-input size="mini"  v-model="dataForm.lastInPrice" placeholder="上一次进价" clearable></el-input>
    </el-form-item>
    <el-form-item label="原零售价" prop="oldRetailPrice">
      <el-input size="mini"  v-model="dataForm.oldRetailPrice" placeholder="原零售价" clearable></el-input>
    </el-form-item>
    <el-form-item label="零售价" prop="retailPrice">
      <el-input size="mini"  v-model="dataForm.retailPrice" placeholder="零售价" clearable></el-input>
    </el-form-item>
    <el-form-item label="原会员价" prop="oldMemberPrice">
      <el-input size="mini"  v-model="dataForm.oldMemberPrice" placeholder="原会员价" clearable></el-input>
    </el-form-item>
    <el-form-item label="会员价" prop="memberPrice">
      <el-input size="mini"  v-model="dataForm.memberPrice" placeholder="会员价" clearable></el-input>
    </el-form-item>
    <el-form-item label="整件数" prop="wholeNum">
      <el-input size="mini"  v-model="dataForm.wholeNum" placeholder="整件数" clearable></el-input>
    </el-form-item>
    <el-form-item label="散装数" prop="spreadNum">
      <el-input size="mini"  v-model="dataForm.spreadNum" placeholder="散装数" clearable></el-input>
    </el-form-item>
    <el-form-item label="门店id" prop="storeId">
      <el-input size="mini"  v-model="dataForm.storeId" placeholder="门店id" clearable></el-input>
    </el-form-item>
    <el-form-item label="门店名称" prop="storeName">
      <el-input size="mini"  v-model="dataForm.storeName" placeholder="门店名称" clearable></el-input>
    </el-form-item>
    <el-form-item label="门店编码" prop="storeCode">
      <el-input size="mini"  v-model="dataForm.storeCode" placeholder="门店编码" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input size="mini"  v-model="dataForm.createTime" placeholder="创建时间" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input size="mini"  v-model="dataForm.updateTime" placeholder="修改时间" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUsername">
      <el-input size="mini"  v-model="dataForm.createUsername" placeholder="创建人" clearable></el-input>
    </el-form-item>
    <el-form-item label="创建人id" prop="createUserId">
      <el-input size="mini"  v-model="dataForm.createUserId" placeholder="创建人id" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateUsername">
      <el-input size="mini"  v-model="dataForm.updateUsername" placeholder="修改人" clearable></el-input>
    </el-form-item>
    <el-form-item label="修改人id" prop="updateUserId">
      <el-input size="mini"  v-model="dataForm.updateUserId" placeholder="修改人id" clearable></el-input>
    </el-form-item>
    <el-form-item label="是否可用 1:是" prop="status">
      <el-input size="mini"  v-model="dataForm.status" placeholder="是否可用 1:是" clearable></el-input>
    </el-form-item>
    <el-form-item label="是否删除 1:是" prop="isDeleted">
      <el-input size="mini"  v-model="dataForm.isDeleted" placeholder="是否删除 1:是" clearable></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input size="mini"  v-model="dataForm.remark" placeholder="备注" clearable></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          itemId: 0,
          orderId: '',
          orderNo: '',
          goodsId: '',
          goodsCode: '',
          goodsName: '',
          genericName: '',
          standard: '',
          unit: '',
          producingArea: '',
          produceFactory: '',
          approvalNo: '',
          dosageForm: '',
          batchNo: '',
          produceDate: '',
          expiredDate: '',
          purchaseNum: '',
          purchasePrice: '',
          purchaseAmount: '',
          lastInPrice: '',
          oldRetailPrice: '',
          retailPrice: '',
          oldMemberPrice: '',
          memberPrice: '',
          wholeNum: '',
          spreadNum: '',
          storeId: '',
          storeName: '',
          storeCode: '',
          createTime: '',
          updateTime: '',
          createUsername: '',
          createUserId: '',
          updateUsername: '',
          updateUserId: '',
          status: '',
          isDeleted: '',
          remark: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '药品Id不能为空', trigger: 'blur' }
          ],
          goodsCode: [
            { required: true, message: '药品编号不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '药品名称不能为空', trigger: 'blur' }
          ],
          genericName: [
            { required: true, message: '通用名不能为空', trigger: 'blur' }
          ],
          standard: [
            { required: true, message: '规格不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          producingArea: [
            { required: true, message: '产地不能为空', trigger: 'blur' }
          ],
          produceFactory: [
            { required: true, message: '生产单位不能为空', trigger: 'blur' }
          ],
          approvalNo: [
            { required: true, message: '批准文号不能为空', trigger: 'blur' }
          ],
          dosageForm: [
            { required: true, message: '剂型不能为空', trigger: 'blur' }
          ],
          batchNo: [
            { required: true, message: '批号不能为空', trigger: 'blur' }
          ],
          produceDate: [
            { required: true, message: '生产日期不能为空', trigger: 'blur' }
          ],
          expiredDate: [
            { required: true, message: '有效期不能为空', trigger: 'blur' }
          ],
          purchaseNum: [
            { required: true, message: '采购数量不能为空', trigger: 'blur' }
          ],
          purchasePrice: [
            { required: true, message: '采购价不能为空', trigger: 'blur' }
          ],
          purchaseAmount: [
            { required: true, message: '采购金额不能为空', trigger: 'blur' }
          ],
          lastInPrice: [
            { required: true, message: '上一次进价不能为空', trigger: 'blur' }
          ],
          oldRetailPrice: [
            { required: true, message: '原零售价不能为空', trigger: 'blur' }
          ],
          retailPrice: [
            { required: true, message: '零售价不能为空', trigger: 'blur' }
          ],
          oldMemberPrice: [
            { required: true, message: '原会员价不能为空', trigger: 'blur' }
          ],
          memberPrice: [
            { required: true, message: '会员价不能为空', trigger: 'blur' }
          ],
          wholeNum: [
            { required: true, message: '整件数不能为空', trigger: 'blur' }
          ],
          spreadNum: [
            { required: true, message: '散装数不能为空', trigger: 'blur' }
          ],
          storeId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          storeName: [
            { required: true, message: '门店名称不能为空', trigger: 'blur' }
          ],
          storeCode: [
            { required: true, message: '门店编码不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          createUsername: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          updateUsername: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          updateUserId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用 1:是不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除 1:是不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.itemId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.itemId) {
            const res = await this.$http({
              url: `/purchase/purchaseorderitem/info/${this.dataForm.itemId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.purchaseOrderItem
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/purchase/purchaseorderitem/${!this.dataForm.itemId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
